package com.stu.boke.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.stu.boke.entity.Category;
import com.stu.boke.service.CategoryService;

@Controller
@RequestMapping("/admin/category")
public class CategoryController {
	
	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping(value = "")
	public ModelAndView indexCategory() {
		ModelAndView model = new ModelAndView();
		List<Category> categoryList = categoryService.listCategoryWithCount();
		model.addObject("categoryList", categoryList);
		model.setViewName("/Admin/Category/index");
		return model;
	}
	
	@RequestMapping(value = "/insertSubmit")
	public String insertSubmitCategory(Category category) {
		categoryService.insertSubmitCategory(category);
		return "redirect:/admin/category";
	}
	
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView editCategoryView(@PathVariable("id") Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		Category category = categoryService.getCategoryById(id);
		modelAndView.addObject("category", category);
		List<Category> categoryWithCount = categoryService.listCategoryWithCount();
		modelAndView.addObject("categoryList", categoryWithCount);
		modelAndView.setViewName("Admin/Category/edit");
		return modelAndView;
	}
	
	@RequestMapping(value = "/editSubmit")
	public String editCategorySubmit(Category category) {
		categoryService.updateCategory(category);
		return "redirect:/admin/category";
	}
	
	@RequestMapping(value = "/delete/{id}")
	public String deleteCategory(@PathVariable("id") Integer id) {
		categoryService.deleteCategory(id);
		return "redirect:/admin/category";
	}
	
}
